package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckitemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/checkgroup")
@Api(tags = "检查组管理接口")
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;
    @Autowired
    private CheckitemService checkitemService;

    /**
     * 新增
     * @param checkitemIds
     * @param checkGroup
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增")
    public Result add(@RequestParam List<String> checkitemIds, @RequestBody CheckGroup checkGroup){
        log.info("新增检查组:{},{}", checkGroup,checkitemIds);
        checkGroupService.add(checkitemIds, checkGroup);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS,null);
    }
    /**
     * 根据id删除检查组
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    @ApiOperation("根据id删除检查组")
    public Result deleteById(@RequestParam Integer id){
        log.info("根据id删除检查组:{}",id);
        checkGroupService.deleteById(id);
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS,null);
    }
    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("findPage")
    @ApiOperation("分页查询")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询:{}", queryPageBean);
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        return pageResult;
    }
    /**
     * 编辑
     * @param checkitemIds
     * @param checkGroup
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation("编辑")
    public Result edit(@RequestParam List<String> checkitemIds, @RequestBody CheckGroup checkGroup){
        log.info("编辑:{},{}", checkGroup,checkitemIds);
        checkGroupService.edit(checkitemIds, checkGroup);
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS,null);
    }
    /**
     * 查询所有检查组
     */
    @GetMapping("/findAll")
    @ApiOperation("查询所有检查组")
    public Result findAll(){
        log.info("查询所有检查组");
        List<CheckGroup> checkGroups = checkGroupService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroups);
    }
    /**
     * 根据id查询检查组
     */
    @GetMapping("/findById")
    @ApiOperation("根据id查询检查组")
    public Result findById(@RequestParam Integer id){
        log.info("根据id查询检查组:{}",id);
        CheckGroup checkGroup = checkGroupService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }
}
